###########################################################################
###########################################################################
############# SCRIPT TO PRODUCE FIGURES A.1, A.5, and A.6 IN ONLINE APPENDIX  ##################
###########################################################################
###########################################################################
###########################################################################

# Authors: 
#   Junyan Jiang (Asst. Prof, Dept of Government and Public Administration, Chinese University of Hong Kong)
#   Yu Zeng (Ph.D candidate, Dept of Political Science, University of Pennsylvania)

# Version: 1.0
# Tested under:
## R version 3.4.4
## Platform: x86_64-w64-mingw32   



#######################################################
################### Preparation #######################
#######################################################

setwd('C:/Users/junya/Dropbox/Research Projects/[A] Land liberalization/Replication')


require(plyr)
require(reshape)
require(reshape2)
library(IRanges)
library(ggplot2)
library(igraph)
library(Cairo)
library(data.table)
library(lubridate)
library(foreign)
library(grid)
library(gridExtra)
library(scales)

facTOchar<-function(data,direction="f2c",exclude=NULL){
  if(direction=="f2c"){
    i<-sapply(data,is.factor)
    data[,i]<-lapply(data[,i],as.character)
    return(data)
  }
  if(direction=="c2f"){
    i<-sapply(data,is.character)
    data[,i]<-lapply(data[,i],function(x) factor(x,exclude=exclude))
    return(data)
  }
  
}




theme_set(theme_bw() + theme(text=element_text(family="Times"),legend.position="bottom",legend.direction="horizontal",axis.title.x=element_text(vjust=-.5),axis.title.y=element_text(vjust=1),plot.title=element_text(face="bold",size=17,vjust=1.5),axis.text.x=element_text(size=12),strip.text=element_text(size=13)))


windowsFonts(Times=windowsFont("TT Times New Roman"))

##---- Functions---##

listdele<-function(x){
  a<-x[complete.cases(x),]
  return(a)
}

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)

# functions for r/cbinding gtables
#source("(FUN) bind gtables.r")


##---- Graphical parameters ----##

pd<-position_dodge(width=0.2)

cl<-c("red","steelblue")


########################################################
##### Figure A.1: Reform Initiatives Across Years ###### ########################################################

dy<-read.dta("policy_year.dta")

gp<-ggplot(data=dy,aes(x=year))+geom_bar()+scale_x_continuous(breaks=seq(2000,2014,2))+
  scale_y_continuous(breaks=seq(2,8,2))+xlab("")+ylab("No. of Cities Adopting Reform")

pdf("policy_no.pdf",width=7,height=4)
gp
dev.off()



########################################################
##### Figure A.5: Reform Initiatives Across Years ###### ########################################################

ds<-na.omit(read.dta("petition_share.dta"))
ds$share<-ds$sub_tp30_16/ds$provcitycountymsg

gp<-ggplot(ds,aes(x=year,y=share))+geom_line()+geom_point()+scale_y_continuous(labels=percent,limits=c(0,0.08))+ylab("Year")+xlab("Share of Land-Related Petitions") 

pdf("petition_share.pdf",width=7,height=4)
gp
dev.off()



########################################################
##### Figure A.6: Validating the Online Petition Measure with Alternative Data on Protest Intensity   ########### ########################################################

d<-read.dta("val_landprotest.dta")
dbar<-ddply(d[which(d$year>=2008 & d$year<=2013),],.(sub_tp30_16),summarise, landprotest=mean(landprotest,na.rm=T))

gv1<-ggplot(data=dbar,aes(x=sub_tp30_16,y=landprotest))+geom_bar(stat="identity")+
  xlab("# of Land Petitions (Online)")+ylab("Avg # of Land-related Protests\n")+ggtitle("Comparison with Protest Dataset")+
  theme(plot.title = element_text(hjust = 0.5))



dsv<-read.dta("val_survey.dta")
dsv<-dsv[-which(dsv$year==2008),]
dsv$year<-dsv$year-1
d2<-merge(dsv,d[,c("sub_tp30_16","cityid","year")],by=c("cityid","year"),all.x=T)

dpro<-ddply(d2,.(sub_tp30_16),summarise, 
            totalprotest2=mean(totalprotest2,na.rm=T),
            totalcontention=mean(totalcontention,na.rm=T))

gv2<-ggplot(data=dpro,aes(x=sub_tp30_16,y=totalprotest2))+geom_bar(stat="identity")+
  xlab("# of Land Petitions  (Online)")+ylab("% of Peasants \n Reporting Participation in \nProtests Last Year")+ggtitle("Comparison with CCAS Survey")+theme(plot.title = element_text(hjust = 0.5)) +scale_y_continuous(labels=percent_format())





pdf("validation.pdf",width=7,height=8)
grid.arrange(gv1,gv2)
dev.off()






